package com.ozo.boot.common.base.util;

import com.ozo.boot.common.base.constant.BaseEnum;

public class BaseEnumUtil {

    /**
     * 通过code获取枚举
     *
     * @param clazz
     * @param code
     * @return
     * @param <T>
     */
    public static <T extends BaseEnum> T fromCode(Class<T> clazz, String code) {
        for (T enumConstant : clazz.getEnumConstants()) {
            if (enumConstant.getCode().equals(code)) {
                return enumConstant;
            }
        }
        throw new IllegalArgumentException("No enum constant with code " + code + " in " + clazz);
    }

    /**
     * 通过code获取枚举描述
     *
     * @param clazz
     * @param code
     * @return
     * @param <T>
     */
    public static <T extends BaseEnum> String descFromCode(Class<T> clazz, String code) {
        for (T enumConstant : clazz.getEnumConstants()) {
            if (enumConstant.getCode().equals(code)) {
                return enumConstant.getDesc();
            }
        }
        throw new IllegalArgumentException("No enum constant with code " + code + " in " + clazz);
    }

}
